<%@page import="com.lgx.common.entity.Dictionary"%>
<%@page import="com.lgx.mall.core.entity.Product"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	
	String type = (String)request.getAttribute("type");
	String productId = (String)request.getAttribute("productId");
	
	Product product = (Product)request.getAttribute("product");
	List<Dictionary> colors = (List<Dictionary>)request.getAttribute("color");
	List<Dictionary> clothessize = (List<Dictionary>)request.getAttribute("clothessize");
	List<Dictionary> shoesize = (List<Dictionary>)request.getAttribute("shoesize");
	List<Dictionary> fareway = (List<Dictionary>)request.getAttribute("fareway");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>添加商品</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<jsp:include page="/page/head.jsp"></jsp:include>
	<script type="text/javascript" src="<%=basePath %>ckeditor/ckeditor.js"></script>
	<script type="text/javascript">
		var type = "<%=type%>";
		var productId = "<%=productId%>";
		
		$(document).ready(function(){
		});
		
		function save(){
			if($('#fm').form('validate')){
				var colorValue = "";
				$("input[name='color']:checked").each(function(){
					if(colorValue != "") colorValue += ",";
					colorValue += $(this).val();
				});
				
				var clothessizeValue = "";
				$("input[name='clothessize']:checked").each(function(){
					if(clothessizeValue != "") clothessizeValue += ",";
					clothessizeValue += $(this).val();
				});
				
				var shoesizeValue = "";
				$("input[name='shoesize']:checked").each(function(){
					if(shoesizeValue != "") shoesizeValue += ",";
					shoesizeValue += $(this).val() ;
				});
				
				var farewayValue = "";
				$("input[name='fareway']:checked").each(function(){
					if(farewayValue != "") farewayValue += ",";
					farewayValue += $(this).val() ;
				});

				var data = "&name="+escape(escape($('#name').val()));
				data += "&areaNode="+$('#areaNode').combobox('getValue');
				data += "&code="+escape(escape($('#code').val()));
				data += "&imageUrl="+escape(escape($('#imageUrl').val()));
				data += "&price="+escape(escape($('#price').val()));
				data += "&leaves="+escape(escape($('#leaves').val()));
				data += "&brand="+$('#brand').combobox('getValue');
				data += "&onsale="+escape(escape($('#onsale').val()));
				data += "&areaCategory="+escape(escape($('#areaCategory').val()));
				data += "&category="+$('#category').combotree('getValue');
				data += "&color="+escape(escape(colorValue));
				data += "&clothessize="+escape(escape(clothessizeValue));
				data += "&shoesize="+escape(escape(shoesizeValue));
				data += "&weight="+escape(escape($('#weight').val()));
				data += "&fareway="+escape(escape(farewayValue));
				data += "&description="+escape(escape(CKEDITOR.instances.description.getData()));
				data += "&farewaydesc="+escape(escape(CKEDITOR.instances.farewaydesc.getData()));
				data += "&saledesc="+escape(escape(CKEDITOR.instances.saledesc.getData()));
				
				var url =  path;
				if(type == "add") {
					url += "/product/save.do?t="+new Date().getTime();
				}else {
					url += "/product/update.do?t="+new Date().getTime();
					url += "&productId="+productId;
				}
				
				$.ajax({
				  url: url,
				  data:data,
				  success: function(data) {
					   if(data == "1")
					   {
						   window.location = path+"/product/productMgr.do";
					   } else {
							alertBox("操作失败！");
					   }
				  }
				});
				
			}
		}
		
		function callback(){
			window.location = path+"/product/productMgr.do";
		}
		
		function selectImage(){
	       	var w =(screen.width-300)/2;
			var h = (screen.height-60)/2;
			var ret = window.showModalDialog(path+"/page/common/image-upload.jsp",window,"dialogWidth:300px;dialogHeight:60px;dialogTop:"+h+"px;dialogLeft:"+w+"px;help:no;status:no;center:yes");
			if(ret != null){
				$('#image').attr('src',path+"/"+ret[0][0]);
				$('#imageUrl').attr('value',ret[0][0]);
			}
		}
	</script>
  </head>
  
  <body>
   <div class="right">
	<h1><b>${type=='add'?'添加商品':'编辑商品' }</b><span><img src="<%=basePath%>images/right_h1bg2.jpg" width="3" height="29" /></span></h1>
	<form id="fm">
	<div class="ri_main">
		<table width="97%" border="0" cellspacing="1" cellpadding="8" style="background:#d9d9d9;">
			<tr>
				<td width="21%" align="right" bgcolor="#FFFFFF">名称：</td>
				<td width="79%" bgcolor="#FFFFFF">
					<input class="easyui-validatebox" name="name" id="name" required="true" value="${product != null?product.name:''}" />
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">同城区域：</td>
				<td bgcolor="#FFFFFF">
					<input class="easyui-combobox" 
						name="areaNode"
						id="areaNode"
						url="${pageContext.request.contextPath}/intra/city/listToCombobox.do" 
						valueField="areaNode" 
						textField="areaName" 
						panelHeight="auto" 
						value="${product != null?product.areaNode:''}"
						required="true" />
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">商品编号：</td>
				<td bgcolor="#FFFFFF">
					<input class="easyui-validatebox" name="code" id="code" required="true" value="${product != null?product.code:''}" />
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">图片：</td>
				<td bgcolor="#FFFFFF">
					<img id="image" width="200" height="200" name="image" src="<%=basePath %>${product != null?product.imageUrl1:''}"></img>
			    	<input type="hidden" id="imageUrl" name="imageUrl" value="${product != null?product.imageUrl1:''}">
			    	<input type="button" onclick="selectImage()" value="选择" />
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">单价：</td>
				<td bgcolor="#FFFFFF">
					<input class="easyui-numberbox" precision="2" name="price" id="price" required="true" value="${product != null?product.price:''}" />
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">商品库存：</td>
				<td bgcolor="#FFFFFF">
					<input class="easyui-numberbox" name="leaves" id="leaves" required="true" value="${product != null?product.leaves:''}" />
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">商品品牌：</td>
				<td bgcolor="#FFFFFF">
					<input class="easyui-combobox" 
							name="brand"
							id="brand"
							url="${pageContext.request.contextPath}/brand/listBrand.do" 
							valueField="id" 
							textField="name" 
							panelHeight="auto" 
							value="${product != null?product.brand.id:''}"
							required="true" />
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">是否上架：</td>
				<td bgcolor="#FFFFFF">
					<select id="onsale" name="onsale">
						<c:forEach var="dict" items="${onsale}">
				   			<c:choose>
				   				<c:when test="${product != null && dict.code == product.onsale}">
				  					<option value="${dict.code}" selected="selected">${dict.name}</option>
				   				</c:when>
				   				<c:otherwise>
				   					<option value="${dict.code}">${dict.name}</option>
				   				</c:otherwise>
				   			</c:choose>
				   		</c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">分区类型：</td>
				<td bgcolor="#FFFFFF">
					<select id="areaCategory" name="areaCategory">
						<c:forEach var="dict" items="${areaCategory}">
				   			<c:choose>
				   				<c:when test="${product != null && dict.code == product.areaCategory}">
				  					<option value="${dict.code}" selected="selected">${dict.name}</option>
				   				</c:when>
				   				<c:otherwise>
				   					<option value="${dict.code}">${dict.name}</option>
				   				</c:otherwise>
				   			</c:choose>
				   		</c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">商品类型：</td>
				<td bgcolor="#FFFFFF">
					<input class="easyui-combotree" 
						name="category"
						id="category"
						url="${pageContext.request.contextPath}/category/listToCombotree.do" 
						value="${product != null?product.category.id:''}"
						required="true" />
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">可选颜色：</td>
				<td bgcolor="#FFFFFF">
					<%
						for(Dictionary dict : colors){
							%><input type="checkbox" name="color" value="<%=dict.getCode() %>" 
		   						   <%
		   						   if(product != null){
		   								for(String color : product.getColor().split(",")){
			   							   if(dict.getCode().equals(color)){
			   								   %>checked="checked"<%
			   							   }
			   						   }
		   						   }
		   						   %> /><%=dict.getName() %><%
						}
					
					%>
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">可选衣服尺码：</td>
				<td bgcolor="#FFFFFF">
					<%
						for(Dictionary dict : clothessize){
							%><input type="checkbox" name="clothessize" value="<%=dict.getCode() %>" 
		   						   <%
		   						   if(product != null){
		   								for(String color : product.getClothessize().split(",")){
			   							   if(dict.getCode().equals(color)){
			   								   %>checked="checked"<%
			   							   }
			   						   }
		   						   }
		   						   %> /><%=dict.getName() %><%
						}
					
					%>
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">可选鞋子尺码：</td>
				<td bgcolor="#FFFFFF">
					<%
						for(Dictionary dict : shoesize){
							%><input type="checkbox" name="shoesize" value="<%=dict.getCode() %>" 
		   						   <%
		   						   if(product != null){
		   								for(String color : product.getShoesize().split(",")){
			   							   if(dict.getCode().equals(color)){
			   								   %>checked="checked"<%
			   							   }
			   						   }
		   						   }
		   						   %> /><%=dict.getName() %><%
						}
					
					%>
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">重量：</td>
				<td bgcolor="#FFFFFF">
					<input class="easyui-numberbox" precision="2" name="weight" id="weight" value="${product != null?product.weight:''}" />公斤
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">可选运费支付方式：</td>
				<td bgcolor="#FFFFFF">
					<%
						for(Dictionary dict : fareway){
							%><input type="checkbox" name="fareway" value="<%=dict.getCode() %>" 
		   						   <%
		   						   if(product != null){
		   								for(String color : product.getFareway().split(",")){
			   							   if(dict.getCode().equals(color)){
			   								   %>checked="checked"<%
			   							   }
			   						   }
		   						   }
		   						   %> /><%=dict.getName() %><%
						}
					
					%>
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">商品描述：</td>
				<td bgcolor="#FFFFFF">
					<textarea rows="40" cols="80" class="ckeditor" name="description">
						${product != null?product.description:'请详细编辑商品描述页面'}
					</textarea>
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">配送说明：</td>
				<td bgcolor="#FFFFFF">
					<textarea rows="40" cols="80" class="ckeditor" name="farewaydesc">
						${product != null?product.farewaydesc:farewaydesc}
					</textarea>
					
				</td>
			</tr>
			<tr>
				<td align="right" bgcolor="#FFFFFF">售后服务：</td>
				<td bgcolor="#FFFFFF">
					<textarea rows="40" cols="80" class="ckeditor" name="saledesc">
						${product != null?product.saledesc:saledesc}
					</textarea>
				</td>
			</tr>
			
			<tr>
				<td align="center" bgcolor="#FFFFFF" colspan="2">
					<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-ok" onclick="save()">保存</a>
					<a href="javascript:void(0);" class="easyui-linkbutton" iconCls="icon-undo" onclick="callback()">取消</a>
				</td>
			</tr>
		</table>
	</div>
	</form>
	</div>
  </body>
</html>
